package com.huffman.smart_picture.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huffman.smart_picture.common.exception.BusinessException;
import com.huffman.smart_picture.common.exception.ErrorEnum;
import com.qcloud.cos.utils.Md5Utils;
import lombok.Getter;


/**
 * @author DengLin
 * @title 翻译工具
 * @date 2025/3/28
 */
public class TranslateUtil {

    private TranslateUtil() {

    }

    @Getter
    public enum LanguageEnum {

        CHINESE("zh", "中文"),

        ENGLISH("en", "英语"),
        ;

        private String language;

        private String description;

        LanguageEnum(String language, String description) {
            this.language = language;
            this.description = description;
        }
    }

    private static final String BAIDU_APP_ID = "";

    private static final String BAIDU_TRANSLATE_SECRET = "";

    private static final String BAIDU_TRANSLATE_URL = "http://api.fanyi.baidu.com/api/trans/vip/translate?";

    /**
     * 翻译
     *
     * @param content 原文内容
     * @param from    原文语种
     * @param to      目标语种
     * @return 失败返回原文内容
     */
    public static String translate(String content, String from, String to) {
        String salt = String.valueOf(System.currentTimeMillis());
        String sign = BAIDU_APP_ID + content + salt + BAIDU_TRANSLATE_SECRET;
        String signMd5 = Md5Utils.md5Hex(sign);

        String url = BAIDU_TRANSLATE_URL + "q=" + content
                + "&from=" + from + "&to=" + to
                + "&appid=" + BAIDU_APP_ID + "&salt=" + salt + "&sign=" + signMd5;

        try {
            // 发起HTTP请求
            HttpResponse response = HttpRequest.get(url).execute();
            if (HttpStatus.HTTP_OK == response.getStatus()) {
                String body = response.body();
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode jsonNode = objectMapper.readTree(body);
                if (jsonNode != null && jsonNode.has("trans_result")) {
                    return jsonNode.get("trans_result").get(0).get("dst").asText();
                }
            }
        } catch (Exception e) {
            throw new BusinessException(ErrorEnum.SYSTEM_ERROR, "调用百度翻译API失败");
        }


        return content;
    }

    /**
     * 中翻英
     *
     * @param content
     * @return
     */
    public static String chineseToEnglish(String content) {
        return translate(content, LanguageEnum.CHINESE.getLanguage(), LanguageEnum.ENGLISH.getLanguage());
    }

    public static String englishToChinese(String content) {
        return translate(content, LanguageEnum.ENGLISH.getLanguage(), LanguageEnum.CHINESE.getLanguage());
    }

    public static void main(String[] args) {
//        for (String key : PictureConstant.CATEGORY) {
//            String res = chineseToEnglish(key);
//            String format = String.format(key + ":" + res);
//            System.out.println(format);
//        }
    }
}
